//index.js
//获取应用实例
const app = getApp()
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../../utils/request.js');
Page({
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    isKeyboard: !1,
    isNumberKB: !1,
    tapNum: !1,
    disableKey: "1234567890港澳学",
    keyboardNumber: "1234567890ABCDEFGHJKLMNPQRSTUVWXYZ港澳学",
    keyboard1: "京沪粤津冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘桂琼渝川贵云藏陕甘青宁新使领警港澳台学",
    inputPlates: {
      index0: "",
      index1: "",
      index2: "",
      index3: "",
      index4: "",
      index5: "",
      index6: "",
      index7: ""
    },
    inputOnFocusIndex: "",
    flag: true,
    carNumber: '',
    carNumberList: [],
    choice: 0,
    newEngine: 0
  },
  onLoad: function () {
    var that = this
    that.getCarNum()
  },
  change(e) {
    var index = e.target.dataset.index
    var that = this
    that.data.choice = index
    that.setData(that.data)
  },
  getCarNum() {
    var that = this
    let openId = app.globalData.openId || wx.getStorageSync('OPENID')
    let nickName = app.globalData.nickName || wx.getStorageSync('nickName')
    requestDataparams('/api/parkInfo/queryLicensePlateList', {
      openId: openId,
      nickName: nickName
    }, 'POST', 'sign').then(response => {
      if (response.data.code == 0) {
        that.setData({
          carNumberList: response.data.data.records
        })
      }
      for (var index in that.data.carNumberList) {
        var licPlateNum = that.data.carNumberList[index].licPlateNum
        licPlateNum = licPlateNum.substring(0, 2) + ' ' + licPlateNum.substring(2, licPlateNum.length)
        that.data.carNumberList[index].licPlateNum = licPlateNum
      }
      that.setData(that.data)
    })
  },
  //切换车牌
  changeplate: function () {
    var that = this;
    that.setData({
      flag: false,
      inputOnFocusIndex: '7',
      //inputPlates: {
      // index0: "",
      // index1: "",
      // index2: "",
      // index3: "",
      // index4: "",
      // index5: "",
      // index6: "",
      //index7: ""
      //},
    })
  },
  inputClick: function (t) {
    var that = this;
    that.setData({
      inputOnFocusIndex: t.target.dataset.id,
      isKeyboard: !0
    })
    "0" == this.data.inputOnFocusIndex ? that.setData({
      tapNum: !1,
      isNumberKB: !1
    }) : "1" == this.data.inputOnFocusIndex ? that.setData({
      tapNum: !1,
      isNumberKB: !0
    }) : that.setData({
      tapNum: !0,
      isNumberKB: !0
    });

  },

  //键盘点击事件
  tapKeyboard: function (t) {
    t.target.dataset.index;
    var a = t.target.dataset.val;

    switch (this.data.inputOnFocusIndex) {
      case "0":
        this.setData({
          "inputPlates.index0": a,
          inputOnFocusIndex: "1",
        });
        break;

      case "1":
        this.setData({
          "inputPlates.index1": a,
          inputOnFocusIndex: "2"
        });
        break;

      case "2":
        this.setData({
          "inputPlates.index2": a,
          inputOnFocusIndex: "3"
        });
        break;

      case "3":
        this.setData({
          "inputPlates.index3": a,
          inputOnFocusIndex: "4"
        });
        break;

      case "4":
        this.setData({
          "inputPlates.index4": a,
          inputOnFocusIndex: "5"
        });
        break;

      case "5":
        this.setData({
          "inputPlates.index5": a,
          inputOnFocusIndex: "6"
        });
        break;

      case "6":
        this.setData({
          "inputPlates.index6": a,
          inputOnFocusIndex: "7"
        });
        break;

      case "7":
        if (this.data.flag == false) {
          this.setData({
            "inputPlates.index7": a,
            inputOnFocusIndex: "7"
          });
        }

    }
    var n = this.data.inputPlates.index0 + this.data.inputPlates.index1 + this.data.inputPlates.index2 + this.data.inputPlates.index3 + this.data.inputPlates.index4 + this.data.inputPlates.index5 + this.data.inputPlates.index6 + this.data.inputPlates.index7

    this.setData({
      carNumber: n
    })

    this.checkedSubmitButtonEnabled();
  },
  //键盘关闭按钮点击事件
  tapSpecBtn: function (t) {
    var a = this,
      e = t.target.dataset.index;
    if (0 == e) {
      switch (parseInt(this.data.inputOnFocusIndex)) {
        case 0:
          this.setData({
            "inputPlates.index0": "",
            inputOnFocusIndex: "0"
          });
          break;

        case 1:
          this.setData({
            "inputPlates.index1": "",
            inputOnFocusIndex: "0"
          });
          break;

        case 2:
          this.setData({
            "inputPlates.index2": "",
            inputOnFocusIndex: "1"
          });
          break;

        case 3:
          this.setData({
            "inputPlates.index3": "",
            inputOnFocusIndex: "2"
          });
          break;

        case 4:
          this.setData({
            "inputPlates.index4": "",
            inputOnFocusIndex: "3"
          });
          break;

        case 5:
          this.setData({
            "inputPlates.index5": "",
            inputOnFocusIndex: "4"
          });
          break;

        case 6:
          this.setData({
            "inputPlates.index6": "",
            inputOnFocusIndex: "5"
          });
          break;

        case 7:
          this.setData({
            "inputPlates.index7": "",
            inputOnFocusIndex: "6"
          });
      }
      this.checkedSubmitButtonEnabled();
    } else 1 == e && a.setData({
      isKeyboard: !1,
      isNumberKB: !1,
      inputOnFocusIndex: ""
    });
  },
  //键盘切换
  checkedKeyboard: function () {
    var t = this;
    "0" == this.data.inputOnFocusIndex ? t.setData({
      tapNum: !1,
      isNumberKB: !1
    }) : "1" == this.data.inputOnFocusIndex ? t.setData({
      tapNum: !1,
      isNumberKB: !0
    }) : this.data.inputOnFocusIndex.length > 0 && t.setData({
      tapNum: !0,
      isNumberKB: !0
    });
  },

  checkedSubmitButtonEnabled: function () {
    this.checkedKeyboard();
    var t = !0;
    for (var a in this.data.inputPlates)
      if ("index7" != a && this.data.inputPlates[a].length < 1) {
        t = !1;
        break;
      }
  },
  submitAddCar() {
    var that = this;
    console.log(that.data.carNumber)
    if (that.data.carNumber.length < 7) {
      wx.showToast({
        title: '请输入正确车牌',
        icon: 'none'
      })
      return
    }
    var openId = app.globalData.openId || wx.getStorageSync('openId');
    requestDataparams('/api/parkInfo/addLicensePlate', {
      openId: openId,
      licPlateNum: that.data.carNumber,
      type: 1
    }, 'POST', 'json').then(response => {

      if (response.data.code == 0) {
        wx.showToast({
          title: '添加成功',
          icon: 'none'
        })
        that.data.inputPlates = {
          index0: "",
          index1: "",
          index2: "",
          index3: "",
          index4: "",
          index5: "",
          index6: "",
          index7: ""
        }
        that.data.isKeyboard = !that.data.isKeyboard
        that.data.flag = true
        that.data.carNumber = ""
        that.setData(that.data)
        that.getCarNum()

      } else {
        wx.showToast({
          title: '失败',
          icon: 'none'
        })
      }
    })
  },
  // 前往支付界面
  goPay() {
    let _this = this
    if (this.data.carNumber.length < 6) {
      wx.showToast({
        title: '请把车牌输入完整',
        icon: 'none'
      })
      return
    }
    requestDataparams('/api/unifiedOrder/queryParkInfo/' + _this.data.carNumber, {}, 'POST').then(response => {

      if (response.data.code == 0) {
        let carInfo = JSON.stringify(response.data.data)
        wx.navigateTo({
          url: '../pay/pay?carInfo=' + carInfo,
        })
      }
    })

  },
  submit() {
    var that = this
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.initPage();
    prevPage.setData({
      currentCarNumber: that.data.carNumberList[that.data.choice].licPlateNum
    })
    wx.navigateBack({
      delta: 1
    })
  }
})